package com.ja.openchat.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import com.ja.openchat.BuildConfig;

import com.ja.openchat.AppViewModelFactory;
import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.databinding.ActivityLoginBinding;
import com.ja.openchat.databinding.FragmentLoginByPhoneBinding;
import com.ja.openchat.ui.chat.PreviewWebViewActivity;
import com.ja.openchat.viewmodels.LoginByPhoneViewModel;
import com.ja.openchat.viewmodels.LoginViewModel;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.model.config.AppConfigInfo;

import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.utils.KLog;


public class LoginActivity extends BaseActivity<FragmentLoginByPhoneBinding, LoginByPhoneViewModel> {
    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.fragment_login_by_phone;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public LoginByPhoneViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
//        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this).get(LoginByPhoneViewModel.class);
    }

    @Override
    public void initViewObservable() {
        viewModel.uc.timerEvent.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                binding.tvCode.setText(s);
                if (getString(R.string.verify_code).equals(s)) {
                    binding.tvCode.setEnabled(true);
                    binding.tvCode.setTextColor(getResources().getColor(R.color.login_btn_bg_color));
                } else {
                    binding.tvCode.setEnabled(false);
                    binding.tvCode.setTextColor(getResources().getColor(R.color.color_text_hint));
                }
            }
        });
    }

    @Override
    public void initData() {
//        viewModel.userName.set("gogo0002");
//        viewModel.password.set("123456");
        AppConfigInfo appConfigInfo = AppConfigInfo.getAppWithoutLoginConfigInfo();
        if(appConfigInfo == null || !appConfigInfo.phone_code_login){
            startContainerActivity(LoginByPasswordFragment.class.getCanonicalName());
            finish();
        }
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        TelegramManager.getTelegramManager().cleanCurrentData();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && data != null) {
            String country = data.getStringExtra("country");
            KLog.d(country);
            viewModel.setCountry(country);
        }
    }

    public void toAgree1(View view){
        PreviewWebViewActivity.loadUrl(this,getString(R.string.user_services_agreement), BuildConfig.KHostUserAgreementAddress);
    }

    public void toAgree2(View view){
        PreviewWebViewActivity.loadUrl(this,getString(R.string.privacy_policy), BuildConfig.KHostPrivacyAddress);

    }
}
